

class ProjectController {    
  
    def index = { redirect(action:list,params:params) }
    
    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        Float sum=0
        for(Project projectInstance: Project.list( params ))
        {
            sum+=projectInstance.calcCurrentBalance()
        }
        [ projectInstanceList: Project.list( params ), projectInstanceTotal: Project.count(), totalCurrentBalance:sum ]
        
        
    }

    def show = {
        def projectInstance = Project.get( params.id )
        if(!projectInstance) {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ projectInstance : projectInstance ] }
    }

    def add_employee = {
        def p = Project.get(params.id);
        ArrayList l = new ArrayList();
        for(Employee e : Employee.list()){
            
            if(!p.members.contains(e)){
                l.add(e);
            }
        }

        [ employeeList: l, projectid: params.id ]
    }

    def save_added_employee = {
        String[] vals = request.getParameterValues("employeeid")
        def projectInstance = Project.get(params.projectid)
        if (projectInstance) {
            for(String v : vals){
                def employee = Employee.get(v);
                if(employee == null) {
                    System.out.println(v + " fel!");
                }
                else{
                    projectInstance.addEmployee(employee);
                }
            }
            if(projectInstance.save(flush:true,validate:false)) {
                if(vals.length>1){
                    flash.message = "New employees added"
                }
                else{
                    flash.message = "New employee added"
                }
            }
            else{
                flash.message = "Cannot add new employee"
                
                projectInstance.errors.each {
                    println (it);
                }
               

            }
            redirect(action:show, id : params.projectid)
        }
        else{
            flash.message = "Project \"${projectInstance.name}\" not found with id ${params.projectid}"
            redirect(action:list)
        }
       
    }

    def delete_employee = {
        int currentId;
        [ employeeList: Project.get(params.id).members, projectid: params.id ]
    }
        
    def save_deleted_employee = {
        String[] del = request.getParameterValues("employeeid")
        def projectInstance = Project.get(params.projectid)
        for(String x : del){
            projectInstance.delEmployee(Employee.get(x))
        }
        flash.message = "Employees deleted from the project"
        redirect(action:show, id : params.projectid)
    }
    def delete = {
        def projectInstance = Project.get( params.id )
        if(projectInstance) {
            try {
                projectInstance.delete(flush:true)
                flash.message = "Project ${projectInstance} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Project ${params} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Project \"${projectInstance.name}\" not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def projectInstance = Project.get( params.id )

        if(!projectInstance) {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ projectInstance : projectInstance ]
        }
    }

    def update = {
        def projectInstance = Project.get( params.id )
        if(projectInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(projectInstance.version > version) {
                    
                    projectInstance.errors.rejectValue("version", "project.optimistic.locking.failure", "Another user has updated this Project while you were editing.")
                    render(view:'edit',model:[projectInstance:projectInstance])
                    return
                }
            }
            projectInstance.properties = params
            if(!projectInstance.hasErrors() && projectInstance.save()) {
                flash.message = "Project ${projectInstance} updated"
                redirect(action:show,id:projectInstance.id)
            }
            else {
                render(view:'edit',model:[projectInstance:projectInstance])
            }
        }
        else {
            flash.message = "Project not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def projectInstance = new Project()
        projectInstance.properties = params
        return ['projectInstance':projectInstance]
    }

    def save = {
        def projectInstance = new Project(params)
        if(!projectInstance.hasErrors() && projectInstance.save()) {
            flash.message = "Project ${projectInstance} created"
            redirect(action:show,id:projectInstance.id)
        }
        else {
            render(view:'create',model:[projectInstance:projectInstance])
        }
    }
}
